package com.example.travelservlet.dynamic.service;

import com.example.travelservlet.dynamic.dao.DynamicMapper;
import com.example.travelservlet.entity.*;
import com.example.travelservlet.oss.controller.OssController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DynamicService {
    private OssController ossController = new OssController();
    @Autowired
    private DynamicMapper dynamicMapper;

    public int insertDynamic(DynamicDto dynamicDto) {
        Dynamic dynamic = new Dynamic();
        //将dto对象中的相同属性复制到实体对象中
        BeanUtils.copyProperties(dynamicDto,dynamic);
        String dynamicId = dynamic.getId();
        dynamic.setUserId(dynamicDto.getUser().getUserId());
        List<String> list = dynamicDto.getFiles();
        if (!list.isEmpty()){
            for (String url : list){
                dynamicMapper.bindDynamicAndImage(dynamicId,url);
            }
        }
        Integer integer = dynamicMapper.insertDynamic(dynamic);
        return integer;
    }

    public List<DynamicDto> selectAllDynamic(String userId) {
        List<DynamicDto> dynamicDtos = dynamicMapper.selectAllDynamic();
        for (DynamicDto dynamicDto : dynamicDtos){
            List<LikeDynamic> like_list = dynamicDto.like_list;
            List<DynamicComment> dynamicCommentList = dynamicMapper.findDynamicCommentByDynamicId(dynamicDto.getId());
            List<DynamicCommentDto> dynamicCommentDtoList = new ArrayList<>();
            for (DynamicComment dynamicComment : dynamicCommentList){
                User user = dynamicMapper.findUserById(dynamicComment.getUserId());
                Dynamic dynamic = dynamicMapper.findDynamicById(dynamicComment.getDynamicId());
                DynamicCommentDto dynamicCommentDto = new DynamicCommentDto();
                BeanUtils.copyProperties(dynamicComment,dynamicCommentDto);
                dynamicCommentDto.setDynamic(dynamic);
                dynamicCommentDto.setUser(user);
                dynamicCommentDtoList.add(dynamicCommentDto);
            }
            dynamicDto.setComments_list(dynamicCommentDtoList);
            if (like_list != null){
                for (LikeDynamic likeListBean : like_list){
                    if (likeListBean.getUserId().equals(userId)){
                        dynamicDto.setIs_like(1);
                        break;
                    }else {
                        dynamicDto.setIs_like(0);
                    }
                }
            }
            String id = dynamicMapper.findUserIdByDynamicId(dynamicDto.getId());
            User user = dynamicMapper.findUserById(id);
            dynamicDto.setUser(user);
            int count = dynamicDto.getLike_list().size();
            dynamicDto.setLikeCount(count);
            dynamicMapper.updateLikeCount(dynamicDto.getId(),count);
        }
        return dynamicDtos;
    }

    public void deleteDynamicById(String dynamicId) {
        dynamicMapper.deleteDynamicById(dynamicId);
    }

    public List<Dynamic> findDynamicByKeyWord(String key) {
        return dynamicMapper.findDynamicByKeyWord(key);
    }

    public List<Dynamic> findAllDynamic(){
        return dynamicMapper.findAll();
    }
    public List<Dynamic> findAllDynamic1(int page,int size){
        int pageIndex = (page-1)*size;
        return dynamicMapper.findAll1(pageIndex,size);
    }

    public List<DynamicPlus> findAllDynamic2(){
        return dynamicMapper.selectDynamicPlus();
    }
    public List<DynamicPlus> findAllDynamic3(int page,int size){
        int pageIndex = (page-1)*size;
        return dynamicMapper.selectDynamicPlus1(pageIndex,size);
    }

    public List<DynamicDto> selectAllMyDynamic(String userId) {
        List<DynamicDto> dynamicDtos = dynamicMapper.selectAllMyDynamic(userId);
        for (DynamicDto dynamicDto : dynamicDtos){
            List<LikeDynamic> like_list = dynamicDto.like_list;
            List<DynamicComment> dynamicCommentList = dynamicMapper.findDynamicCommentByDynamicId(dynamicDto.getId());
            List<DynamicCommentDto> dynamicCommentDtoList = new ArrayList<>();
            for (DynamicComment dynamicComment : dynamicCommentList){
                User user = dynamicMapper.findUserById(dynamicComment.getUserId());
                Dynamic dynamic = dynamicMapper.findDynamicById(dynamicComment.getDynamicId());
                DynamicCommentDto dynamicCommentDto = new DynamicCommentDto();
                BeanUtils.copyProperties(dynamicComment,dynamicCommentDto);
                dynamicCommentDto.setDynamic(dynamic);
                dynamicCommentDto.setUser(user);
                dynamicCommentDtoList.add(dynamicCommentDto);
            }
            dynamicDto.setComments_list(dynamicCommentDtoList);
            if (like_list != null){
                for (LikeDynamic likeListBean : like_list){
                    if (likeListBean.getUserId().equals(userId)){
                        dynamicDto.setIs_like(1);
                        break;
                    }else {
                        dynamicDto.setIs_like(0);
                    }
                }
            }
            String id = dynamicMapper.findUserIdByDynamicId(dynamicDto.getId());
            User user = dynamicMapper.findUserById(id);
            dynamicDto.setUser(user);
        }

        System.out.println("others的后端：" + dynamicDtos);
        System.out.println("others传过来的id：" + userId);
        return dynamicDtos;
    }
    public List<DynamicDto> selectAllDynamicByLike(String userId) {
        dynamicMapper.selectAllDynamic();
        List<DynamicDto> dynamicDtos = dynamicMapper.selectAllDynamicByLike();
        for (DynamicDto dynamicDto : dynamicDtos){
            List<LikeDynamic> like_list = dynamicDto.like_list;
            List<DynamicComment> dynamicCommentList = dynamicMapper.findDynamicCommentByDynamicId(dynamicDto.getId());
            List<DynamicCommentDto> dynamicCommentDtoList = new ArrayList<>();
            for (DynamicComment dynamicComment : dynamicCommentList){
                User user = dynamicMapper.findUserById(dynamicComment.getUserId());
                Dynamic dynamic = dynamicMapper.findDynamicById(dynamicComment.getDynamicId());
                DynamicCommentDto dynamicCommentDto = new DynamicCommentDto();
                BeanUtils.copyProperties(dynamicComment,dynamicCommentDto);
                dynamicCommentDto.setDynamic(dynamic);
                dynamicCommentDto.setUser(user);
                dynamicCommentDtoList.add(dynamicCommentDto);
            }
            dynamicDto.setComments_list(dynamicCommentDtoList);
            if (like_list != null){
                for (LikeDynamic likeListBean : like_list){
                    if (likeListBean.getUserId().equals(userId)){
                        dynamicDto.setIs_like(1);
                        break;
                    }else {
                        dynamicDto.setIs_like(0);
                    }
                }
            }
            String id = dynamicMapper.findUserIdByDynamicId(dynamicDto.getId());
            User user = dynamicMapper.findUserById(id);
            dynamicDto.setUser(user);
        }
        return dynamicDtos;
    }
    public int deleteDynamicByDynamicId(String dynamicId) {
        dynamicMapper.deleteCommentById(dynamicId);
        dynamicMapper.deleteLikeById(dynamicId);
        return dynamicMapper.deleteDynamicByDynamicId(dynamicId);
    }

    public List<DynamicDto> selectAllMyDynamicAndLike(String userId, String mineId) {
        List<DynamicDto> dynamicDtos = dynamicMapper.selectAllMyDynamic(userId);
        for (DynamicDto dynamicDto : dynamicDtos){
            List<LikeDynamic> like_list = dynamicDto.like_list;
            List<DynamicComment> dynamicCommentList = dynamicMapper.findDynamicCommentByDynamicId(dynamicDto.getId());
            List<DynamicCommentDto> dynamicCommentDtoList = new ArrayList<>();
            for (DynamicComment dynamicComment : dynamicCommentList){
                User user = dynamicMapper.findUserById(dynamicComment.getUserId());
                Dynamic dynamic = dynamicMapper.findDynamicById(dynamicComment.getDynamicId());
                DynamicCommentDto dynamicCommentDto = new DynamicCommentDto();
                BeanUtils.copyProperties(dynamicComment,dynamicCommentDto);
                dynamicCommentDto.setDynamic(dynamic);
                dynamicCommentDto.setUser(user);
                dynamicCommentDtoList.add(dynamicCommentDto);
            }
            dynamicDto.setComments_list(dynamicCommentDtoList);
            if (like_list != null){
                for (LikeDynamic likeListBean : like_list){
                    if (likeListBean.getUserId().equals(mineId)){
                        dynamicDto.setIs_like(1);
                        break;
                    }else {
                        dynamicDto.setIs_like(0);
                    }
                }
            }
            String id = dynamicMapper.findUserIdByDynamicId(dynamicDto.getId());
            User user = dynamicMapper.findUserById(id);
            dynamicDto.setUser(user);
        }

        System.out.println("others的后端：" + dynamicDtos);
        System.out.println("others传过来的id：" + userId);
        return dynamicDtos;
    }

    public List<DynamicPlus> findDynamicByKeyWord1(String key,Integer page,Integer size) {
        Integer pageIndex = (page-1)*size;
        return dynamicMapper.findDynamicByKeyWord3(key,pageIndex,size);
    }
}
